package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.Export;
import cn.itcast.domain.cargo.Packing;
import cn.itcast.domain.cargo.PackingExample;
import cn.itcast.service.cargo.ExportProductService;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.service.cargo.PackingService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;

@Controller
@RequestMapping("/cargo/packing")
@Slf4j
public class PackingController extends BaseController {

    @Reference
    private PackingService packingService;
    @Reference
    private ExportService exportService;
    @Reference
    private ExportProductService exportProductService;

    /**
     * 1.装箱单列表
     */
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "5") Integer pageSize) {
        log.info("执行报运单列表查询开始...");
        // 构造条件对象
        PackingExample packingExample = new PackingExample();
        // 根据create_time降序排序
        packingExample.setOrderByClause("create_time desc");
        // 获取条件的集合
        PackingExample.Criteria criteria = packingExample.createCriteria();
        // 添加条件
        criteria.andCompanyIdEqualTo(getLoginCompanyId());
        // 分页查询
        PageInfo<Packing> pageInfo = packingService.findByPage(packingExample, pageNum, pageSize);
        request.setAttribute("pageInfo", pageInfo);
        return "cargo/export/export-list";
    }

    /**
     * 3. 合同管理列表，选中购销合同，点击报运，进入报运单添加页面
     * @param id 购销合同的id，多个id自动以逗号隔开
     * @return
     */

    @RequestMapping("/toPacking")
    public String toPacking(String id){
        request.setAttribute("id",id);
        return "cargo/export/export-toPacking";
    }

    /**
     * 4. 报运单添加
     */
    @RequestMapping("/toAdd")
    public String toAdd(Export export) {
        // 设置企业id、企业名称
        export.setCompanyId(getLoginCompanyId());
        export.setCompanyName(getLoginCompanyName());
        export.setCreateTime(new Date());
        exportService.save(export);
        return "redirect:/cargo/export/list";
    }



}
